package com.pcbly.web.components.hibernate.support;

import java.io.Serializable;
import java.util.List;

import com.pcbly.web.Constants;

public class Page<T> extends QueryParameter implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5867995407091403631L;

	private List<T> result = null;

	private int totalCount = -1;// 总记录数
	
	private int totalPages;

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public Page() {
	}
	/**
	 * 默认构造方法.
	 *
	 * @param start	 本页数据在数据库中的起始位置
	 * @param totalSize 数据库中总记录条数
	 * @param pageSize  本页容量
	 * @param data	  本页包含的数据
	 */
	public Page(long start, int totalSize, int pageSize, List<T> data) {
		this.pageSize = pageSize;
		this.start = start;
		this.totalCount = totalSize;
		this.result = data;
	}
	public Page(int pageSize) {
		this.pageSize = pageSize;
	}

	public Page(int pageSize, boolean autoCount) {
		this.pageSize = pageSize;
		this.autoCount = autoCount;
	}

	public String getInverseOrder() {
		if (order.endsWith(DESC))
			return ASC;
		else
			return DESC;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public int getTotalCount() {
		if(totalCount==0){
			if(!(result==null||result.isEmpty()))
			totalCount=result.size();
		}
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	// 取总页数.
	public Integer getTotalPages() {
		if (totalCount == -1)
			return -1;

		int count = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			count++;
		}
		this.totalPages=count;
		return new Integer(this.totalPages);
	}

	/**
	 * 该页是否有下一页.
	 */
	public boolean hasNextPage() {
		return (pageNo + 1 <= getTotalPages());
	}

	public int getNextPage() {
		if (hasNextPage())
			return pageNo + 1;
		else
			return pageNo;
	}

	/**
	 * 取该页当前页码,页码从1开始.
	 */
	public long getCurrentPageNo() {
		return pageNo;
	}

	/**
	 * 该页是否有上一页.
	 */
	public boolean hasPreviousPage() {
		return (pageNo - 1 >= 1);
	}

	public int getPrePage() {
		if (hasPreviousPage())
			return pageNo - 1;
		else
			return pageNo;
	}

	public String toString() {
		return "[" + (hasPreviousPage() ? getPrePage() : 0) + "<<]" + " page["
				+ getPageNo() + "]" + " total[" + getTotalPages() + "]pages "
				+ " [" + getPageSize() + "]items/page " + " total["
				+ getTotalCount() + "]items " + "[>>"
				+ (hasNextPage() ? getNextPage() : 0) + "]";
	}

	/**
	 * 获取任一页第一条数据在数据集的位置，每页条数使用默认值.
	 * 
	 * @see #getStartOfPage(int,int)
	 */
	protected static int getStartOfPage(int pageNo) {
		return getStartOfPage(pageNo, Constants.DEFAULT_PAGE_SIZE);
	}

	/**
	 * 获取任一页第一条数据在数据集的位置.
	 * 
	 * @param pageNo
	 *            从1开始的页号
	 * @param pageSize
	 *            每页记录条数
	 * @return 该页第一条数据
	 */
	public static int getStartOfPage(int pageNo, int pageSize) {
		return (pageNo - 1) * pageSize;
	}
}